import * as compose from 'koa-compose'
import * as Emitter from 'events'


export default class Application extends Emitter {
  middleware: any[] = [];
  context: { path: string; method: string } = Object.create({ path: '/get', method: 'GET' });
  constructor() {
    super();
  }
  use(fn) {
    if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');
    this.middleware.push(fn);
    return this;
  }
  run() {
    const fn = compose(this.middleware);
    const ctx = this.createContext();
    return fn(ctx).catch((e) => {
      console.log(e)
    });
  }
  createContext() {
    const context = Object.create(this.context);
    return context;
  }

};